//
//  MarketMainView.cpp
//  FruitCrush
//
//  Created by gaoyu on 15/7/28.
//
//

#include "SystemMainView.h"
#include "LocalizedStringGetter.h"
#include "MarketMasonryView.h"
#include "MarketGoldView.h"
#include "AchievementView.h"
#include "GoldViewMenuItem.h"

USING_NS_CC;

#define TITLE_HEIGHT   40
#define MENU_ITEM_Y_PAD  5
#define BACK_MENU_WIDTH 50
#define TEXT_FONT_SIZE  22
#define TEXT_FONT_NAME  "arial"

typedef enum {
	MarketMenu1 = 1,
	MarketMenu2,
	MarketBack,
} MenuTag;


typedef enum {
    ZBg,
    ZTitle,
    ZMenu,
} ZIndex;


Scene *SystemMainView::createScene()
{
    auto scene = Scene::create();
    auto layer = SystemMainView::create();
    scene->addChild(layer);
    return scene;
}

bool SystemMainView::init()
{
    if (!Layer::init()) {
        return false;
    }
    bgInit();
    menuInit();
    titleInit();
    return true;
}

void SystemMainView::menuInit()
{
	Size winsize = Director::getInstance()->getWinSize();

    auto cache = SpriteFrameCache::getInstance();
	cache->addSpriteFramesWithFile("CommonIcon.plist");
    auto cache1 = SpriteFrameCache::getInstance();
	cache1->addSpriteFramesWithFile("Diamond.plist");
    auto cache2 = SpriteFrameCache::getInstance();
	cache2->addSpriteFramesWithFile("Achieves.plist");



	auto menuItem1 = GoldViewMenuItem::create("DiamondShopMenu.png", "DiamondShopMenuSelected.png","DiamondShopMenu.png",
			CC_CALLBACK_1(SystemMainView::menuCallback, this));
	menuItem1->setTag(MarketMenu1);
	menuItem1->setTitle(LocalizedStringGetter::getStringByKey("AchieveString"));
	menuItem1->setIcon("AchieveIcon.png");
	menuItem1->setAnchorPoint(Vec2::ZERO);
	menuItem1->setPosition(Point(0, winsize.height - menuItem1->getSize().height*1 - MENU_ITEM_Y_PAD -TITLE_HEIGHT));

	auto menuItemBack = MenuItemImage::create("WinBackButton.png", "WinBackButton.png","WinBackButton.png",
			CC_CALLBACK_1(SystemMainView::menuCallback, this));
	menuItemBack->setTag(MarketBack);
	menuItemBack->setAnchorPoint(Vec2::ZERO);
	menuItemBack->setPosition(Point(winsize.width - BACK_MENU_WIDTH,winsize.height - BACK_MENU_WIDTH));

    auto menu = Menu::create(menuItem1,menuItemBack, NULL);
    menu->setAnchorPoint(Vec2::ZERO);
    menu->setPosition(Vec2::ZERO);
    addChild(menu,ZMenu);

}

void SystemMainView::titleInit()
{
	Size winsize = Director::getInstance()->getWinSize();

	__String *fileName = __String::createWithFormat("ShopTitleBg.png");
	auto title_bg = Sprite::createWithSpriteFrameName(fileName->getCString());
	title_bg->setAnchorPoint(Vec2::ZERO);
	title_bg->setPosition(Point(0, winsize.height - TITLE_HEIGHT));
	this->addChild(title_bg,ZTitle);

    LabelTTF *label = LabelTTF::create(LocalizedStringGetter::getStringByKey("AchieveString"),
    		TEXT_FONT_NAME, TEXT_FONT_SIZE);
    label->setAnchorPoint(Point(0, 0));
    label->setPosition((winsize.width-label->getContentSize().width)/2,
    		winsize.height - TITLE_HEIGHT + (TITLE_HEIGHT-label->getContentSize().height)/2);
    label->setColor(Color3B::BLACK);
    addChild(label,ZTitle);


}

void SystemMainView::menuCallback(Ref *sender)
{
	GoldViewMenuItem *seletedMenu = (GoldViewMenuItem*)sender;

	if(seletedMenu->getTag() == MarketMenu1){
    	auto scene = AchievementView::createScene();
        Director::getInstance()->pushScene(scene);
	} else if(seletedMenu->getTag() == MarketBack){
		Director::getInstance()->popScene();
	}
}

void SystemMainView::bgInit()
{
    auto bg = Sprite::create("DiamondScreenBg.png");
    bg->setAnchorPoint(Vec2::ZERO);
    bg->setPosition(Vec2::ZERO);
    this->addChild(bg);
}
